/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.servlet;

import com.util.SQLConnection;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
/**
 *
 * @author chetans
 */


public class GetTask extends HttpServlet {

    private Connection con;
    private ResultSet rs;
    private Logger myLogger = Logger.getLogger("RootLogger");

    private void getTaskList(PrintWriter out,String week)throws Exception {
       myLogger.info("Enetering..");
       PreparedStatement pstmt;
	   con = new SQLConnection().getMySQLConnection();
       pstmt = con.prepareStatement("select * from task_records where week like ? ");
       pstmt.setString(1, week);
       rs = pstmt.executeQuery();
       String outPut ="<tasks>";
       boolean noRecords = true;
       while(rs.next()){
           noRecords=false;
           outPut=outPut+"<task ";
           outPut = outPut+"name=\""+rs.getString("task_name")+"\" ";
           outPut = outPut+"day1=\""+rs.getInt("day1")+"\" ";
		   outPut = outPut+"day2=\""+rs.getInt("day2")+"\" ";
		   outPut = outPut+"day3=\""+rs.getInt("day3")+"\" ";
		   outPut = outPut+"day4=\""+rs.getInt("day4")+"\" ";
		   outPut = outPut+"day5=\""+rs.getInt("day5")+"\" ";
		   outPut = outPut+"day6=\""+rs.getInt("day6")+"\" ";
		   outPut = outPut+"day7=\""+rs.getInt("day7")+"\" ";
           outPut = outPut+"/>";
      }
       outPut = outPut+"</tasks>";
       myLogger.info(outPut);
      if(noRecords)
          outPut=null;
      out.println(outPut);
      myLogger.info("Exiting..");
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        myLogger.info("Enetering..");
        PrintWriter out = response.getWriter();
        try {
            String week = (String)request.getParameter("week");
            getTaskList(out,week);
        } catch (Exception e) {
            e.printStackTrace();
            myLogger.error("Exception in DB transaction.");
        }
        myLogger.info("Exiting..");
    }
}
